/**
 * OVOrder.java
 * 
 * Copyright@2015 OVT Inc. All rights reserved. 
 * 
 * 2015年12月25日
 */
package com.ovt.order.util;

import java.util.ArrayList;
import java.util.List;

import org.springframework.web.client.RestTemplate;

import com.ovt.order.util.entity.*;

/**
 * OVOrder
 * 
 * @Author leo.liu
 * @Version 1.0
 * @See
 * @Since [OVT Cloud Platform]/[API] 1.0
 */
public class OVPayment
{
    /**
     * PaymentService Base Url
     */
    public static String BASE_URL = "http://localhost:18080/PaymentService/api";

    private static RestTemplate restTemplate = new RestTemplate();

    /**
     * create order
     * 
     * @param order : the object that will be saved.
     * @return process result (json)
     */
    public static Order createOrder(Order order)
    {
        String url = BASE_URL + "/orders/create";

        JsonDocument result = restTemplate.postForObject(url, order,
                JsonDocument.class);

        Order orderResult = null;
        try
        {
            String jsonString = JsonUtils.toJson(result.getData());
            orderResult = JsonUtils.fromJson(jsonString, Order.class);
        }
        catch (OVTException e)
        {
            e.printStackTrace(); // 如何处理异常待定
        }

        return orderResult;
    }

    /**
     * cancel order
     * 
     * @param userId : user id in third-party system
     * @param orderNo : order number
     * @return process result (json)
     */
    public static boolean cancelOrder(String userId, String orderNo)
    {
        String url = BASE_URL + "/orders/cancel";
        String userIdString = "userId=" + userId;
        String orderNoString = "orderNo=" + orderNo;
        url += "?" + userIdString + "&" + orderNoString;

        JsonDocument result = restTemplate.postForObject(url, null,
                JsonDocument.class);

        return ((Boolean) result.getData()).booleanValue();
    }

    /**
     * delete order
     * 
     * @param userId : user id in third-party system
     * @param orderNo : order number
     * @return process result (json)
     */
    public static boolean deleteOrder(String userId, String orderNo)
    {
        String url = BASE_URL + "/orders/delete";
        String userIdString = "userId=" + userId;
        String orderNoString = "orderNo=" + orderNo;
        url += "?" + userIdString + "&" + orderNoString;

        JsonDocument result = restTemplate.postForObject(url, null,
                JsonDocument.class);

        return ((Boolean) result.getData()).booleanValue();
    }

    /**
     * get order list
     * 
     * @param userId : user id in third-party system
     * @return process result (json)
     */
    public static List<Order> getOrderList(String userId)
    {
        String url = BASE_URL + "/orders/list";
        String userIdString = "userId=" + userId;
        url += "?" + userIdString;

        JsonDocument result = restTemplate
                .getForObject(url, JsonDocument.class);

        Object data = result.getData();

        List<Order> orders = new ArrayList<Order>();
        if (data instanceof List)
        {
            List dataList = (List) data;

            for (Object entry : dataList)
            {
                String jsonString;
                try
                {
                    jsonString = JsonUtils.toJson(entry);
                    Order order = JsonUtils.fromJson(jsonString, Order.class);
                    orders.add(order);
                }
                catch (OVTException e)
                {
                    e.printStackTrace(); // 如何处理异常待定
                }
            }
        }
        return orders;
    }

    public static Order getOrderDetail(String userId, String orderNo)
    {
        String url = BASE_URL + "/orders/detail";
        String userIdString = "userId=" + userId;
        String orderNoString = "orderNo=" + orderNo;
        url += "?" + userIdString + "&" + orderNoString;

        JsonDocument result = restTemplate
                .getForObject(url, JsonDocument.class);

        Order orderResult = null;
        try
        {
            String jsonString = JsonUtils.toJson(result.getData());
            orderResult = JsonUtils.fromJson(jsonString, Order.class);
        }
        catch (OVTException e)
        {
            e.printStackTrace(); // 如何处理异常待定
        }

        return orderResult;
    }

    public static Payment getPaymentDetail(String orderNo)
    {
        return null;
    }

    public static PaymentLog getPaymentLog(String orderNo)
    {
        return null;
    }

    public static List<PaymentLog> getPaymentLogList()
    {
        return null;
    }

    public static RefundRequest applyForRefund(String userId,
            RefundRequest refundRequest)
    {
        String url = BASE_URL + "/orders/applyRefund";

        JsonDocument result = restTemplate.postForObject(url, refundRequest,
                JsonDocument.class);

        RefundRequest refundRequestResult = null;
        try
        {
            String jsonString = JsonUtils.toJson(result.getData());
            refundRequestResult = JsonUtils.fromJson(jsonString,
                    RefundRequest.class);
        }
        catch (OVTException e)
        {
            e.printStackTrace(); // 如何处理异常待定
        }

        return refundRequestResult;
    }

    public static boolean refund(List<Long> ids)
    {
        return true;
    }

    public static List<RefundRequest> getRefundRequestList()
    {
        String url = BASE_URL + "/orders/refundList";

        JsonDocument result = restTemplate
                .getForObject(url, JsonDocument.class);

        Object data = result.getData();

        List<RefundRequest> refundRequests = new ArrayList<RefundRequest>();
        if (data instanceof List)
        {
            List dataList = (List) data;

            for (Object entry : dataList)
            {
                String jsonString;
                try
                {
                    jsonString = JsonUtils.toJson(entry);
                    RefundRequest order = JsonUtils.fromJson(jsonString,
                            RefundRequest.class);
                    refundRequests.add(order);
                }
                catch (OVTException e)
                {
                    e.printStackTrace(); // 如何处理异常待定
                }
            }
        }
        return refundRequests;
    }

    public static List<RefundRequest> getRefundRequest(String orderNo)
    {
        return null;

    }

    public static Refund getRefundDetail(String orderNo)
    {
        return null;

    }

    public static RefundLog getRefundLog(long logId)
    {
        return null;

    }

    public static List<RefundLog> getRefundLogList()
    {
        return null;

    }
}
